package com.st.shopadmin.controller;

import com.st.shopadmin.annotation.LoginAccess;
import com.st.shopadmin.common.Const;
import com.st.shopadmin.common.ServerResponse;
import com.st.shopadmin.pojo.AdminManagerUser;
import com.st.shopadmin.pojo.ManagerUser;
import com.st.shopadmin.pojo.User;
import com.st.shopadmin.service.UserManagerService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/manage/user/")
public class UserManagerController {

    @Autowired
    private UserManagerService userService;

    /**
     * class_name: UserManagerController
     * package: com.st.shopadmin.controller
     * describe: 跳转至登录页
     * author: duhongpeng
     * date: 2018/10/24
     * time: 16:00
     **/
    @RequestMapping("/to_login.do")
    public String toLogin() {
        return "user/login";
    }

    @RequestMapping("/to_list.do")
    public String toList() {
        return "manageruser/list";
    }

    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse getUserList(HttpServletRequest httpServletRequest, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,@RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        ServerResponse<List> serverResponse = userService.getUserList(pageNum,pageSize);
        serverResponse.setTotal(serverResponse.getData().size());
        return userService.getUserList(pageNum,pageSize);
    }

    @RequestMapping("page.do")
    @ResponseBody
    @LoginAccess
    public ServerResponse getPageLists(HttpServletRequest httpServletRequest, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,@RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        return userService.getPageLists(pageNum,pageSize);
    }

    /**
     * class_name: UserManagerController
     * package: com.st.shopadmin.controller
     * describe: 登录
     * author: duhongpeng
     * date: 2018/10/24
     * time: 16:00
     **/
    @RequestMapping(value = "do_login.do" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<AdminManagerUser> login(HttpServletResponse httpServletResponse,String username, String password, HttpSession session){
        ServerResponse<AdminManagerUser> response = userService.login(username,password);
        if(response.isSuccess()){
            AdminManagerUser user = response.getData();
            session.setAttribute(Const.CURRENT_USER,user);
        }
        return  response;
    }

    /**
     * class_name: UserManagerController
     * package: com.st.shopadmin.controller
     * describe: 退出登录
     * author: duhongpeng
     * date: 2018/10/24
     * time: 16:00
     **/
    @RequestMapping(value = "do_logout.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> logout(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess();
    }

    /**
     * class_name: UserManagerController
     * package: com.st.shopadmin.controller
     * describe: 跳转至注册页面
     * author: duhongpeng
     * date: 2018/10/24
     * time: 16:00
     **/
    @RequestMapping("/to_register.do")
    public String toRegister() {
        return "user/register";
    }

    /**
     * class_name: UserManagerController
     * package: com.st.shopadmin.controller
     * describe: 注册
     * author: duhongpeng
     * date: 2018/10/24
     * time: 16:00
     **/
   @RequestMapping(value = "register.do",method = RequestMethod.POST)
    @ResponseBody
    public  ServerResponse<String> register(AdminManagerUser user){
        return  userService.register(user);
    }

    /**
     * class_name: UserManagerController
     * package: com.st.shopadmin.controller
     * describe: 获取个人信息
     * author: duhongpeng
     * date: 2018/10/24
     * time: 16:00
     **/
    @RequestMapping(value = "get_user_info.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> getUserInfo(HttpSession session,HttpServletRequest httpServletRequest){
          User user = (User) session.getAttribute(Const.CURRENT_USER);
          if(null != user){
              return ServerResponse.createBySuccess(user);
          }else{
              return  ServerResponse.createByErrorMessage("用户未登录,暂无法获取信息");
          }
    }

    /**
     * class_name: UserManagerController
     * package: com.st.shopadmin.controller
     * describe: 重置密码
     * author: duhongpeng
     * date: 2018/11/1
     * time: 14:29
     **/
    @RequestMapping("restPassword.do")
    @ResponseBody
    public ServerResponse restPassword(HttpServletRequest httpServletRequest,Long Id){
        return userService.restPassword(Id);
    }

    /**
     * class_name: UserManagerController
     * package: com.st.shopadmin.controller
     * describe: 激活账号
     * author: duhongpeng
     * date: 2018/11/1
     * time: 14:29
     **/
    @RequestMapping("activation.do")
    @ResponseBody
    public ServerResponse activation(HttpServletRequest httpServletRequest,Long Id,Integer status){
        return userService.activation(Id,status);
    }

    /**
     * class_name: UserManagerController
     * package: com.st.shopadmin.controller
     * describe: 冻结账号
     * author: duhongpeng
     * date: 2018/11/1
     * time: 14:29
     **/
    @RequestMapping("frozen.do")
    @ResponseBody
    public ServerResponse frozen(HttpServletRequest httpServletRequest,Long Id,Integer status){
        return userService.frozen(Id,status);
    }
}
